home *** CD-ROM | disk | FTP | other *** search
- Path: fourier.newcastle.edu.au!peter
- From: peter@fourier.newcastle.edu.au (Peter Moylan)
- Newsgroups: comp.lang.modula2
- Subject: Re: Other Modula-2 Problems
- Date: 27 Feb 1996 22:41:48 GMT
- Organization: The University of Newcastle
- Message-ID: <4h01bc$8k2@seagoon.newcastle.edu.au>
- References: <313171a2.15277270@news-stand.acs.ohio-state.edu>
- Reply-To: peter@tesla.newcastle.edu.au
- NNTP-Posting-Host: fourier.newcastle.edu.au
- X-Newsreader: TIN [version 1.2 PL2]
-
- M. Klepcyk (mjk+@osu.edu) wrote:
-
- >I have some other questions also. I have to make a program that uses
- >infinite series to approximate the value of SIN (a). The line that my
- >teacher gave me for this is: "sin(a)=a-a3/3!+a5/5!-a7/7!+a9/9!...."
- >What does this translate to in the way of a formula? I need to know
- >this so I can write a procedure that will use the variable that will
- >obviously be involved.
-
- Come on, this is too easy. You should work this one out
- for yourself.
-
- >Another problem concerns writing a procedure that will computer the
- >binary XOR's of two decimal numbers. I know how to get the binary
- >values for each of the decimal numbers (using MOD & DIV), but how do I
- >compare the two binary numbers when I'm finished with this? Is there
- >some kind of comparison command like with strings?
-
- This, however, deserves more comment. I've noticed that
- students often get confused over what is meant by "binary number"
- and "decimal number" in the context of programming.
-
- The names "binary" and "decimal" refer to the _representation_
- of a number, as distinct from the underlying object which is the
- number itself. You're probably aware that "101 binary" and
- "5 decimal" are not two distinct numbers, but instead are two
- alternative ways of writing the _same_ number. It's a bit like
- saying that "pomme" and "apple" are two ways of saying the
- same thing: different languages are used, but despite the
- different words we're still talking about the same fruit.
-
- When manipulating numbers in a computer program, we have to be
- aware of a number of different concepts:
-
- 1. The number itself. This is an abstract object which can
- be defined by, for example, a mathematician's set of axioms
- for arithmetic. I can't write down an example, because as
- soon as I'd written it down I would have written a
- representation of the number, rather than the number itself.
-
- 2. The representation of the number inside the computer's
- memory. For most present-day computers this is a binary
- representation - or, more precisely, it is a collection
- of electrical signals which in turn are representations of
- binary digits - but it doesn't have to be. A hardware
- designer could, if he wished, build a computer in which
- ternary representations were used internally.
-
- 3. The representation of the number in the source code of
- your computer program. This depends on the rules of the
- programming language. For most programming languages a
- decimal notation is used, but there are other possibilities.
- (Modula-2 also has a way to use octal or hexadecimal
- notation.) The important point is that the representation
- doesn't have to be - and usually isn't - the same as the
- representation in 2 above. The compiler takes care of
- translating the notation.
-
- 4. The representation of the number in the external interface;
- for example, the way the number is typed at the keyboard, or the
- way it is printed on the screen. This depends on the I/O
- routines you use. The I/O routines most commonly used
- perform a translation between an external notation
- (usually decimal) and an internal representation (usually
- binary). But that's not compulsory, you could write your
- own formatting procedures that used any base you liked.
-
- This might all sound like quibbling, but it has a bearing on
- your question. Let's look at the question again. You said
- "compute the binary XORs of two decimal numbers". Right there,
- the problem is badly posed. What it should say is something
- like "compute the binary XORs of two numbers". They're
- numbers, not decimal numbers, and the distinction is important.
- There's no such thing as a decimal number. (Although,
- informally, we often use the phrase "decimal number" as a
- shorthand way of saying "decimal representation of a number"
- - which in some ways is a pity, because it's this sloppy
- terminology which is the root of the confusion.)
-
- When you write a statement like "abc := 23", is abc a decimal
- number? Most decidedly not! The character string "23" is the
- decimal representation of a number, but that's where it ends.
- The variable abc holds a number - not a decimal number - and,
- depending on your computer hardware, the internal representation
- is probably binary.
-
- Now, this doesn't give an answer to your original question.
- I don't want to give an answer, because I think it's
- something that you have to work out for yourself. But
- I'll at least give you a hint: if you're thinking of using
- DIV and MOD to convert between decimal and binary, then
- you're completely on the wrong track. You shouldn't have
- to do any conversions.
-
- --
- Peter Moylan peter@ee.newcastle.edu.au
- http://www.eng.newcastle.edu.au/ee/Moylan.html
- OS/2 freeware list at
- http://www.eng.newcastle.edu.au/ee/Moylan/os2/os2info.html
-